package cn.bdqn.controller;

import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultCode;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.bean.utils.JWTUtils;
import cn.bdqn.domain.Doctor;
import cn.bdqn.domain.Nurse;
import cn.bdqn.service.LoginService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.print.Doc;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/loginController")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    @CrossOrigin
    public Result login(String username, String password, Map<String,Object> map) throws Exception {

        System.out.println(username);
        System.out.println(password);
        //获取subject认证对象 连接到自定义的Realm
        Subject subject = SecurityUtils.getSubject();
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            //实现登录认证
            subject.login(new UsernamePasswordToken(username,password));
            //获取登陆成功后的用户信息
            Doctor doctor = loginService.queryDoctorByID(username);
            Nurse nurse = loginService.queryNurseByID(username);
            //判断信息是否存在
            if (doctor != null){
                //返回登录成功的对象
                map.put("doctor",doctor);
                Map<String, String> paramMap = new HashMap<>();
                redisTemplate.boundValueOps(doctor.getDepartmentsId()).set(doctor.getDoctorNumber());
                paramMap.put("id", doctor.getDoctorNumber());
                paramMap.put("name", doctor.getDoctorName());
                //登陆成功后生成一个验证token  jwt加密认证
                String token = JWTUtils.getToken(paramMap);
                map.put("token",token);
                return ResultGenerator.genSuccessResult(map).setCode(ResultCode.SUCCESS);
            }
            if (nurse != null){
                map.put("nurse",nurse);
                Map<String, String> paramMap = new HashMap<>();
                redisTemplate.boundValueOps(nurse.getDeptmentId()).set(nurse.getCard());
                paramMap.put("id", nurse.getCard());
                paramMap.put("name", nurse.getName());
                String token = JWTUtils.getToken(paramMap);
                map.put("token",token);
                return ResultGenerator.genSuccessResult(map).setCode(ResultCode.SUCCESS);
            }
            return ResultGenerator.genFailResult("登陆成功");
        }catch (UnknownAccountException e){
            //用户名错误时的的异常
            e.printStackTrace();
            System.out.println("用户名错误");
            return ResultGenerator.genFailResult("用户名错误").setCode(ResultCode.INTERNAL_SERVER_ERROR);
        }catch (IncorrectCredentialsException e){
            //密码错误的异常
            e.printStackTrace();
            System.out.println("密码错误");
            return ResultGenerator.genFailResult("密码错误").setCode(ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 注册医生
     * @param doctor
     * @return
     * @throws Exception
     */
    @RequestMapping("/registerDoctor")
    public Result registerDoctor(Doctor doctor) throws Exception{

        try {
            int num = loginService.addDoctor(doctor);
            if (num == 0){
                return ResultGenerator.genSuccessResult("工号重复");
            }else{
                return ResultGenerator.genSuccessResult("添加成功");
            }
        }catch (Exception e){
            return ResultGenerator.genFailResult("出现异常!");
        }
    }

    /**
     * 注册护士
     * @param nurse
     * @return
     * @throws Exception
     */
    @RequestMapping("/registerNurse")
    public Result registerNurse(Nurse nurse) throws Exception {

        try{
            loginService.addNurse(nurse);
            return ResultGenerator.genSuccessResult("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("出现异常!");
        }
    }
}
